package com.itheima.reggie.controller;

import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.service.CategoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 菜品分类分页查询
     */
    @GetMapping("/{page}")
    public R<Page> page(int page, int pageSize){
        //分页查询构造器
        Page<Category> Infopage = new Page(page,pageSize);
        //条件查询器
        LambdaQueryWrapper<Category> lqw = new LambdaQueryWrapper<>();
        //通过sort降序查询
        lqw.orderByDesc(Category::getSort);
        categoryService.page(Infopage,lqw);
        //返回查询结果
        return R.success(Infopage);
    }

    /**
     * 套餐，菜品添加
     * @param category
     * @return
     */
    @PostMapping
    public R<String> save(@RequestBody Category category){
        categoryService.save(category);
        return R.success("添加数据成功");
    }

    /**
     * 套餐，菜品信息修改
     * @param category
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return R.success("修改信息成功");
    }

    /**
     * 菜品，套餐信息删除
     * @param id
     * @return
     */
    @DeleteMapping
    public R<String> delete(Long id){
        categoryService.removeById(id);
        return R.success("删除成功");
    }

    /**
     * 根据分类 类型 获取所有分类列表
     * @param category
     * @return
     */
    @GetMapping("/list")
    public R<List<Category>> list(Category category){
        LambdaQueryWrapper<Category> lqw=new LambdaQueryWrapper<>();
        lqw.eq(category.getType()!=null,Category::getType,category.getType());
        //根据排序标识 倒序排序
        lqw.orderByDesc(Category::getUpdateTime);
        List<Category> list = categoryService.list(lqw);
        return R.success(list);
    }

}
